//////////////////////////////////////////////////////////////////////////
//@author LM 
//2013/2/7 version1.0

//定义 HashMap类 模仿Java中HashMap类的使用 待扩展//////////////////////////////////////////////////
/////////////////  使用示例  var map = new HashMap();
function HashMap(){
	
	/*属性*/
	this.key = new Array(); //定义属性 类型为数组
    this.value = new Array();
	
	/*方法*/
	this.put = function(k,v){//定义方法  添加键值对
		var index = this.index(k);
		if(index == -1){
	       this.key.push(k);
		   this.value.push(v);
		}else{
		   this.value[index] = v;
		}
	};
	
	this.get = function(k,flag){ //根据键取值 flag true返回并删除元素  false 返回元素不删除
	    var index = this.index(k);
		if(-1 == index)
		    return null;
		
	    if(!flag){
		   return this.value[index];
		}else{
			var returnv = this.value[index];
			this.key.splice(index,1);
		    this.value.splice(index,1);
			return returnv;
		}
	}
	this.index = function(k){//返回指定键的索引
		for(var i = 0 ; i < this.key.length ; i++){//保证键唯一
		    if(this.key[i] == k)
			      return i;
		}
		return -1;
	}
	
	
	this.getKey = function(index){//根据索引获取键
		return this.key[index];
	}
	
	this.keyLike = function(keyV){//判断集合中是否包含指定的字符串的键
		var size = this.size();
		for(var i=0;i<size;i++){
			if(this.getKey(i).indexOf(keyV) != -1)
			    return true;
		}
		return false;
	}
	
	this.clear = function(){//清空
		this.key.splice(0,this.key.length);
		this.value.splice(0,this.value.length);
	}
	
	this.size = function(){//集合元素数量
		if(this.key.length == this.value.length)
		   return this.key.length;
		else
		   return -1;
	}
	
	this.remove = function(index){//删除指定索引的的元素
	    if(index < 0)
		    return;
		if(this.key.length == this.value.length){
			if(this.key.length >= (index+1)){
			  this.key.splice(index,1);
			  this.value.splice(index,1);
			}
		}
	}
	
	this.del = function(k){//根据键删除Map里对应的元素
		var index = this.index(k);
		if(-1 == index)
		    return false;
	    this.remove(index);
		return true;
	}
	
	this.rever = function(){//反序
		this.key.reverse();
		this.value.reverse();
	}
	
	this.insert = function(index,flag,nk,nv){//在指定的位置插入键值 flag true后插 false前插 nk键 nv值
		
			var leftk;
			var rightk;
			var leftv;
			var rightv;
			var index = this.index(nk);
			if(index >= 0){
			   this.value[index] = nv;
			   return;
			}
			var num = (flag?(index+1):index);
			leftk = this.key.slice(0,num);
			rightk = this.key.slice(num,this.key.length);
			leftv = this.value.slice(0,num);
			rightv = this.value.slice(num,this.value.length);
			
			var nkarr = new Array(1);
			nkarr[0] = nk;
			leftk = leftk.concat(nkarr,rightk);
			this.key = leftk;
			
			var nvarr = new Array(1);
			nvarr[0] = nv;
			leftv = leftv.concat(nvarr,rightv);
			this.value = leftv;
	}
	
	this.show = function(){//打印当前集合中的键值对
		if(this.key.length == this.value.length){
			var showstr = "[";
		    for(var i = 0; i < this.key.length; i++){
			     showstr += "{" + (this.key[i] + ":" + this.value[i]) + "}"
				 if(i < (this.key.length - 1)){
					 showstr += ",";
				 }
			}
			showstr += "]";
			return showstr;
		}else{
			return "can not show!";
		}
	}
	
}